<template>
	<body-wrap isFlex>
		<view class="topSearch">
			<my-search v-model="kwd" @confirm='doSearch'/>
		</view>
		<view class="zjListBox">
			<radio-group @change="radioChange">
				<view class="zjListItem" v-for="dt in teacherlist" :key="dt._id">
					<view class="rdbox"><radio :value="dt._id" :checked="selectId==dt._id" /></view>
					<view class="tcInfo"><min-teacher-info :infor="dt"/></view>
					<navigator class="lk" :url="`/pages/tDetail/index?id=${dt._id}`">
						<i class="iconfont icon-xiangyoujiantou"></i>
					</navigator>
				</view>
			</radio-group>
			<empty v-if="teacherlist && teacherlist.length==0"/>
		</view>
		<template #footer>
			<div class="footer-btn-box">
				<view class="myBtn" @click="confirm">确认求助</view>
			</div>
		</template>
	</body-wrap>
</template>

<script>
	import {defineComponent,reactive,toRefs,onMounted,getCurrentInstance,computed} from 'vue'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import bodyWrap from "@/components/bodyWrap/index.vue"
	import mySearch from "@/components/mySearch/index.vue"
	import teacherList from "@/components/teacherList/index.vue"
	import {getUserList,editQuestion,getQuestionDetail} from "@/api/consult.js"
	import empty from "@/components/empty/index.vue"
	import minTeacherInfo from "@/components/minTeacherInfo/index.vue"
	import {toast} from "@/utils/common.js"
	export default defineComponent({
		components:{
			bodyWrap,
			mySearch,
			teacherList,
			empty,
			minTeacherInfo
		},
		props:{
		
		},
		setup(props,context) {
			const store = useStore()
			const {proxy} = getCurrentInstance()
			
			const state = reactive({
				selectTeacher:computed(()=>store.state.base.selectTeacher),
				userInfo:computed(()=>store.state.base.userInfo),
				kwd:'',
				pageIndex:1,
				pageSize:20,
				teacherlist:'',
				selectId:'',
				qId:''
			})
			
			onLoad(options=>{
				if(options && options.keyword){
					state.kwd = options.keyword
				}
				if(options && options.qid){
					state.qId = options.qid
				}
				getTcList()
			})
			
			onMounted(()=>{
				
			})
			
			const getTcList = async ()=>{
				const res = await getUserList({
					type:1,
					pageIndex:state.pageIndex,
					pageSize:state.pageSize,
					from:'EJConsult',
					keywords:state.kwd
				})
				state.teacherlist = res.data.data || []
				if(state.selectTeacher){
					state.selectId = state.selectTeacher._id
				}
			}
			
			const doSearch = ()=>{
				getTcList()
			}
			
			const radioChange = (e) =>{
				state.selectId = e.detail.value
			}
			
			const confirm = async ()=>{	
				const slData = state.teacherlist.find(item =>item._id == state.selectId)
				if(!state.selectId || !slData){
					return toast('请选择需要求助的专家')
				}
				const res2 = await getQuestionDetail(state.qId)
				if(res2.code !=200){
					return toast('求助失败，请重试！')
				}
				const qaDetail = res2.data
				if(!qaDetail.transferList || qaDetail.transferList.length<1){
					qaDetail.transferList = [state.userInfo._id,state.selectId]
				}else{
					qaDetail.transferList.push(state.selectId)
				}
				const res = await editQuestion({
					id:state.qId,
					params:{
						teacherId:slData._id,
						transferList:qaDetail.transferList
					}
				})
				if(res.code ==200){
					toast('求助成功!')
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/home/teacher/customQa/index'
						})
					},2000)
				}else{
					toast('求助失败，请重试！')
				}
			}
			
			return {
				...toRefs(state),
				doSearch,
				radioChange,
				confirm
			}
		}
	})
</script>

<style lang="scss" scoped>
	.topSearch{
		padding: 20rpx;
	}
	.zjListBox{
		.zjListItem{
			display: flex;
			box-sizing: border-box;
			width: 100%;
			padding: 20rpx;
			margin: 0 0 20rpx 0;
			background: #fff;
			.tcInfo{
				flex: 1;
				flex-shrink: 0;
			}
			.rdbox{
				display: flex;
				align-items: center;
				flex-shrink: 0;
				margin: 0 8rpx 0 0;
			}
			.lk{
				display: flex;
				align-items: center;
				flex-shrink: 0;
			}
		}
	}
	
</style>
